#include "windows.h"
#include "OpenMaxAlCtrl.h"


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
    }
    return TRUE;
}

#define WIDE_VIDEO_WINDOW_WIDTH  640
#define WIDE_VIDEO_WINDOW_HEIGHT 360
#define VIDEO_WINDOW_WIDTH  480
#define VIDEO_WINDOW_HEIGHT 360
#define WIDE_VIDEO_SUBTITLE_WINDOW_WIDTH  640
#define WIDE_VIDEO_SUBTITLE_WINDOW_HEIGHT 100
#define VIDEO_SUBTITLE_WINDOW_WIDTH  480
#define VIDEO_SUBTITLE_WINDOW_HEIGHT 100
#define FULL_VIDEO_SUBTITLE_WINDOW_WIDTH  800
#define FULL_VIDEO_SUBTITLE_WINDOW_HEIGHT 200
#define SECONDARY_VIDEO_WINDOW_WIDTH  720
#define SECONDARY_VIDEO_WINDOW_HEIGHT 576
#define SECONDARY_SCREEN_X_OFFSET     800

typedef enum
{
    ePlayer_init = 0,
    ePlayer_playing,
    ePlayer_paused,
	ePlayer_stopped,
	ePlayer_deinit
} PLAYER_STATE;

// Global Variables:

PLAYER_STATE  gPlayerState = ePlayer_init;
BOOL  gMute = FALSE;
XAmillisecond gDuration = 0;
XAmillisecond gCurrentMediatime = 0;
UINT  gTimerID=0;
BOOL  gbCanSeek = TRUE;
static int gScreenWidth = 0;
static int gScreenHeight = 0;
int gVideoWidth = 0;
int gVideoHeight = 0;
BOOL gbFullscreen = FALSE;
BOOL gbWidescreen = FALSE;
BOOL gbPlayerDestroy = TRUE;
BOOL gbAudioOnly = FALSE;
BOOL gbVideoOutputSubSreen = FALSE;
BOOL gbVideoOutputMainSreen = TRUE;
BOOL gbSubtitleEnable = FALSE;
BOOL gbPlaybackLoop = FALSE;
BOOL gbAppMinimazed = FALSE;
int gAudioTrackid = 0;
int gSubtitleTrackid = 0;
BOOL gbSubScreenOutputFormatPAL = TRUE;


//Start of OpenMAX AL Interface
HANDLE  hMutex;
// engine interfaces
static XAObjectItf engineObject = NULL;
static XAEngineItf engineEngine = NULL;

// metadataextraction interfaces
static XAObjectItf metadataObj = NULL;
static XAMetadataExtractionItf metadataExtractionItf = NULL;

// output mix interfaces
static XAObjectItf outputMixObject = NULL;

// media player interfaces
static XAObjectItf playerObj = NULL;
static XAPlayItf   playerPlayItf = NULL;
static XAStreamInformationItf  playerStreamInfoItf = NULL;
static XASeekItf  playerSeekItf = NULL;
static XAVolumeItf             playerVolItf = NULL;
static XAVideoPostProcessingItf    playerVideoPostProcessItf = NULL;
void Player_callback (XAPlayItf caller, void * pContext, XAuint32 event, XAresult result, XAuint32 param, void * pInterface);
XAuint32 scaleOption = XA_VIDEOSCALE_STRETCH;
//End of OpenMAX AL Interface
/* registered player callback. */

void Player_callback (XAPlayItf caller, void * pContext, XAuint32 event)
{
    switch (event)
    {
		case XA_PLAYEVENT_HEADATEND :
			 RETAILMSG(TRUE,(L"playback hit the end\n"));
/*
			 if (FALSE == gbPlaybackLoop)
			 {
				stop();
				StopTimer();
				gPlayerState = ePlayer_stopped;
				SetDlgItemText( gDiagBoxhWnd,IDC_PLAY,L"PLAY");
				seek(0);
  			    SendDlgItemMessage (gDiagBoxhWnd, IDC_MEDIA_SEEK, TBM_SETPOS,TRUE,0);
			 }
			 else
			 {
				StopTimer();
				gCurrentMediatime = 0;
				SendDlgItemMessage (gDiagBoxhWnd, IDC_MEDIA_SEEK, TBM_SETPOS,TRUE,0);
				StartTimer(TIMER_PERIOD);
			 }
*/
			 break;

		case XA_PLAYEVENT_HEADATMARKER :
			 RETAILMSG(TRUE,(L"playback hit the marker.\n"));
			break;

		case XA_PLAYEVENT_HEADATNEWPOS :
			RETAILMSG(TRUE,(L"playback at new position.\n"));
			break;

		case XA_PLAYEVENT_HEADMOVING :
			RETAILMSG(TRUE,(L"playback is active.\n"));
			break;

		case XA_PLAYEVENT_HEADSTALLED :
			RETAILMSG(TRUE,(L"playback stalled.\n"));
/*
            if (*((XAuint32 *)pContext) == XA_RESULT_BUFFER_INSUFFICIENT)
				MessageBox(NULL,L"Filter:Out of memory",0,0);
			else if (*((XAuint32 *)pContext) == XA_RESULT_CONTENT_UNSUPPORTED)
				MessageBox(NULL,L"Filter:Content not supported",0,0);
			destroyMediaPlayerObj();
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_MAINENABLE),FALSE);
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_SUBENABLE),FALSE);
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_MAINDISABLE),FALSE);
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_SUBDISABLE),FALSE);
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_SUBTITLE_ENABLE),FALSE);
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_SUBTITLE_DISABLE),FALSE);
            SendDlgItemMessage(gDiagBoxhWnd,IDC_AUDIO_TRACK,CB_RESETCONTENT,NULL,NULL);
            SendDlgItemMessage(gDiagBoxhWnd,IDC_SUBTITLE_LIST,CB_RESETCONTENT,NULL,NULL);
            SendDlgItemMessage (gDiagBoxhWnd, IDC_MEDIA_SEEK, TBM_SETPOS,TRUE,0);
            SetDlgItemText(gDiagBoxhWnd,IDC_MEDIA_TIME,L"0:00:00");
            SetDlgItemText(gDiagBoxhWnd,IDC_DURATION,L"0:00:00");
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_PAL),FALSE);
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_NTSC),FALSE);
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_SUBTITLE_LIST),FALSE);
            EnableWindow(GetDlgItem(gDiagBoxhWnd,IDC_AUDIO_TRACK),FALSE);
            CheckDlgButton(gDiagBoxhWnd,IDC_MUTE,BST_UNCHECKED);
            CheckDlgButton(gDiagBoxhWnd,IDC_REPEAT,BST_UNCHECKED);
			SetDlgItemText( gDiagBoxhWnd,IDC_PLAY,L"PLAY");
*/
			break;

		default :
			RETAILMSG(TRUE, (TEXT("bogus callback event : %d\r\n"),(int)event));
			break;
    }
}

static BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
{
    DWORD dwMinSize;
    dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
    if(dwSize < dwMinSize)
    {
        return FALSE;
    }
    WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
    return TRUE;
}

static BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize,BOOL utf8)
{
    // Get the required size of the buffer that receives the Unicode
    // string.
    DWORD dwMinSize;
    if(utf8 == TRUE)
        dwMinSize = MultiByteToWideChar (CP_UTF8, 0, lpcszStr, -1, NULL, 0);
    else
        dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);

    if(dwSize < dwMinSize)
    {
        return FALSE;
    }

    // Convert headers from ASCII to Unicode.
    if(utf8 == TRUE)
        MultiByteToWideChar (CP_UTF8, 0, lpcszStr, -1, lpwszStr, dwMinSize);
    else
        MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);
    return TRUE;
}
void OPENMAXAL_InitMediaResource()
{
	OPENMAXAL_CreateMediaEngine();
}
void OPENMAXAL_UninitMediaResource()
{
	OPENMAXAL_StopFile();
	OPENMAXAL_DestroyMediaPlayerObj();
}
void OPENMAXAL_CreateMediaEngine(void)
{
    XAresult res;

    // create engine
    res = xaCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
    ASSERT(XA_RESULT_SUCCESS == res);

    // realize the engine
    res = (*engineObject)->Realize(engineObject, XA_BOOLEAN_FALSE);
    ASSERT(XA_RESULT_SUCCESS == res);

    // get the engine interface, which is needed in order to create other objects
    res = (*engineObject)->GetInterface(engineObject, XA_IID_ENGINE, &engineEngine);
    ASSERT(XA_RESULT_SUCCESS == res);

    // create output mix
    res = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
    ASSERT(XA_RESULT_SUCCESS == res);

    // realize the output mix
    res = (*outputMixObject)->Realize(outputMixObject, XA_BOOLEAN_FALSE);
    ASSERT(XA_RESULT_SUCCESS == res);

}
BOOL OPENMAXAL_CreateMediaPlayerObj(LPCWSTR filepath, HWND MainVWhnd,HWND SubVWhnd)
{
	XAresult res;
    XADataSource audioSource;
    XADataLocator_URI uri;
    XADataFormat_MIME mime;
    XAchar uri_path[512];
    XADataSource dataSrc;


    WCharToMByte(filepath,uri_path,sizeof(uri_path)/sizeof(uri_path[0]));

	uri.locatorType = XA_DATALOCATOR_URI;
    uri.pURI = uri_path;
   // mime.formatType = XA_DATAFORMAT_MIME;
   // mime.pMimeType = (XAchar *) "audio/ogg";
   // mime.containerType = XA_CONTAINERTYPE_WAV;
    audioSource.pLocator = (void*) &uri;
    audioSource.pFormat = (void*) &mime;

    // configure data source
  //  XADataFormat_MIME format_mime = {XA_DATAFORMAT_MIME, XA_ANDROID_MIME_MP2TS, XA_CONTAINERTYPE_MPEG_TS };
	dataSrc.pLocator = (void*) &uri;
    dataSrc.pFormat = (void*) &mime;
    // configure audio sink
    XADataLocator_OutputMix outmix = { XA_DATALOCATOR_OUTPUTMIX, outputMixObject };
    XADataSink AudioSnk = { &outmix, NULL };

    // configure image video sink
    XADataLocator_NativeDisplay VideoWindow = {
            XA_DATALOCATOR_NATIVEDISPLAY,        // locatorType
            (void*)MainVWhnd,                    // MainScreen video window
            (void*)SubVWhnd                     // SubScreen video window
    };
    XADataSink VideoSink = {&VideoWindow, NULL};

    // declare interfaces to use
    XAboolean     required[5]
                           = {XA_BOOLEAN_TRUE,XA_BOOLEAN_TRUE,XA_BOOLEAN_TRUE,XA_BOOLEAN_TRUE,XA_BOOLEAN_TRUE};
    XAInterfaceID iidArray[5]
                           = {XA_IID_PLAY,XA_IID_SEEK,XA_IID_STREAMINFORMATION,XA_IID_VOLUME,XA_IID_VIDEOPOSTPROCESSING};

    // create media player
    res = (*engineEngine)->CreateMediaPlayer(engineEngine, &playerObj, &dataSrc,
            NULL, &AudioSnk, &VideoSink, NULL, NULL,
            2 /*XAuint32 numInterfaces*/,
            iidArray /*const XAInterfaceID *pInterfaceIds*/,
            required /*const XAboolean *pInterfaceRequired*/);
    ASSERT(XA_RESULT_SUCCESS == res);

    // realize the player
    res = (*playerObj)->Realize(playerObj, XA_BOOLEAN_FALSE);
    if(XA_RESULT_SUCCESS != res)
	{
		return FALSE;
	}

    // get the play interface
    res = (*playerObj)->GetInterface(playerObj, XA_IID_PLAY, &playerPlayItf);
    ASSERT(XA_RESULT_SUCCESS == res);

    // get the stream information interface (for video size)
    res = (*playerObj)->GetInterface(playerObj, XA_IID_STREAMINFORMATION, &playerStreamInfoItf);
    ASSERT(XA_RESULT_SUCCESS == res);

    // get the volume interface
    res = (*playerObj)->GetInterface(playerObj, XA_IID_VOLUME, &playerVolItf);
    ASSERT(XA_RESULT_SUCCESS == res);

    // get the seek interface
    res = (*playerObj)->GetInterface(playerObj, XA_IID_SEEK, &playerSeekItf);
    ASSERT(XA_RESULT_SUCCESS == res);

    // get the videopostprocessing interface
    res = (*playerObj)->GetInterface(playerObj, XA_IID_VIDEOPOSTPROCESSING, &playerVideoPostProcessItf);
    ASSERT(XA_RESULT_SUCCESS == res);

    /* Register callback. */
    res = (*playerPlayItf)->RegisterCallback(playerPlayItf, Player_callback, NULL);
    ASSERT(XA_RESULT_SUCCESS == res);

    /* only interested in EOF. */
    res = (*playerPlayItf)->SetCallbackEventsMask(playerPlayItf, XA_PLAYEVENT_HEADATEND|XA_PLAYEVENT_HEADSTALLED);
    ASSERT(XA_RESULT_SUCCESS == res);


    return TRUE;
}
BOOL OPENMAXAL_CreateMetadataObj(LPCWSTR filepath)
{
	XAresult res;
    XADataLocator_URI uri;
    XADataFormat_MIME mime;
    XAchar uri_path[512];
    XADataSource dataSrc;


    WCharToMByte(filepath,uri_path,sizeof(uri_path)/sizeof(uri_path[0]));

	uri.locatorType = XA_DATALOCATOR_URI;
    uri.pURI = uri_path;

    // configure data source
  //  XADataFormat_MIME format_mime = {XA_DATAFORMAT_MIME, XA_ANDROID_MIME_MP2TS, XA_CONTAINERTYPE_MPEG_TS };
	dataSrc.pLocator = (void*) &uri;
    dataSrc.pFormat = (void*) &mime;

    // declare interfaces to use
    XAboolean     required[1]= {XA_BOOLEAN_TRUE};
    XAInterfaceID iidArray[1]= {XA_IID_METADATAEXTRACTION};

    // create media player
    res = (*engineEngine)->CreateMetadataExtractor(engineEngine, &metadataObj, &dataSrc,
            1 /*XAuint32 numInterfaces*/,
            iidArray /*const XAInterfaceID *pInterfaceIds*/,
            required /*const XAboolean *pInterfaceRequired*/);
    if(XA_RESULT_SUCCESS != res)
    {
        return FALSE;
    }

    // realize the player
    res = (*metadataObj)->Realize(metadataObj, XA_BOOLEAN_FALSE);
    if(XA_RESULT_SUCCESS != res)
    {
        return FALSE;
    }
    // get the play interface
    res = (*metadataObj)->GetInterface(metadataObj, XA_IID_METADATAEXTRACTION, &metadataExtractionItf);
    if(XA_RESULT_SUCCESS != res)
    {
        return FALSE;
    }

    return TRUE;
}
void OPENMAXAL_Play()
{
    XAresult res;
    res = (*playerPlayItf)->SetPlayState(playerPlayItf, XA_PLAYSTATE_PLAYING);
    ASSERT(XA_RESULT_SUCCESS == res);
}
void OPENMAXAL_PlayFile()
{
	if (ePlayer_init == gPlayerState)
	{
		gPlayerState = ePlayer_playing;
	}
	else if (ePlayer_playing == gPlayerState)
	{
		gPlayerState = ePlayer_paused;
	}
	else if (ePlayer_paused == gPlayerState || ePlayer_stopped == gPlayerState)
	{
		gPlayerState = ePlayer_playing;
	}
}
void OPENMAXAL_OpenFile(LPCWSTR filepath, HWND MainVWhnd,HWND SubVWhnd)
{
	if (FALSE == gbPlayerDestroy)
		OPENMAXAL_DestroyMediaPlayerObj();
	if (OPENMAXAL_CreateMediaPlayerObj(filepath,MainVWhnd,SubVWhnd)==FALSE)
	{
		return;
	}
	gbPlayerDestroy = FALSE;

	OPENMAXAL_GetVideoSize();
	OPENMAXAL_SetVideoOriginalScreen();
	if (ePlayer_init == gPlayerState)
	{
		OPENMAXAL_Play();
		gPlayerState = ePlayer_playing;
//		OPENMAXAL_SetPlaybackRepeat(gbPlaybackLoop);
	}
}
void OPENMAXAL_Stop()
{
	XAresult res;
    res = (*playerPlayItf)->SetPlayState(playerPlayItf, XA_PLAYSTATE_STOPPED);
    ASSERT(XA_RESULT_SUCCESS == res);
}
void OPENMAXAL_StopFile()
{
	if (ePlayer_playing == gPlayerState || ePlayer_paused == gPlayerState)
	{
		gPlayerState = ePlayer_stopped;
	}
}

void OPENMAXAL_PauseFile()
{
	XAresult res;
	res = (*playerPlayItf)->SetPlayState(playerPlayItf, XA_PLAYSTATE_PAUSED);
    ASSERT(XA_RESULT_SUCCESS == res);
}

void OPENMAXAL_SetVolume(long volume)
{
	XAresult res;
    res = (*playerVolItf)->SetVolumeLevel(playerVolItf, (XAint16)volume);
    ASSERT(XA_RESULT_SUCCESS == res);
}

void OPENMAXAL_SetMute(BOOL bMute)
{
	XAresult res;
	if (bMute)
		res = (*playerVolItf)->SetMute(playerVolItf, XA_BOOLEAN_TRUE);
	else
		res = (*playerVolItf)->SetMute(playerVolItf, XA_BOOLEAN_FALSE);

	ASSERT(XA_RESULT_SUCCESS == res);
}

void OPENMAXAL_GetDuration(XAmillisecond *duration)
{
	XAresult res;
	*duration = 0;
	res = (*playerPlayItf)->GetDuration(playerPlayItf, duration);

    ASSERT(XA_RESULT_SUCCESS == res);
}

XAresult OPENMAXAL_GetPosition(XAmillisecond *position)
{
	XAresult res;
	*position = 0;
	res = (*playerPlayItf)->GetPosition(playerPlayItf, position);
	return res;
    //ASSERT(XA_RESULT_SUCCESS == res);
}

XAresult OPENMAXAL_Seek(XAmillisecond pos)
{
	XAresult res;
	res = (*playerSeekItf)->SetPosition(playerSeekItf, pos ,XA_SEEKMODE_ACCURATE);
    return res;
}

void OPENMAXAL_GetVideoSize()
{
	XAresult res;
	XAMediaContainerInformation MediaInfo;
	XAuint32  DomainType;

	res = (*playerStreamInfoItf)->QueryMediaContainerInformation(playerStreamInfoItf, &MediaInfo);
    ASSERT(XA_RESULT_SUCCESS == res);

	for (int i=0;i<MediaInfo.numStreams;i++)
	{
		res = (*playerStreamInfoItf)->QueryStreamType(playerStreamInfoItf,i,&DomainType);
		ASSERT(XA_RESULT_SUCCESS == res);
		if (XA_DOMAINTYPE_VIDEO == DomainType)
		{
			XAVideoStreamInformation video_stream_info;
			res = (*playerStreamInfoItf)->QueryStreamInformation(playerStreamInfoItf, i, &video_stream_info);
			ASSERT(XA_RESULT_SUCCESS == res);
			RETAILMSG(TRUE, (TEXT("AP Set Video video codec id [%d]\r\n"),video_stream_info.codecId));
			if (video_stream_info.codecId)
			{
				gVideoHeight= video_stream_info.height;
				gVideoWidth = video_stream_info.width;
				RETAILMSG(TRUE, (TEXT("AP Set Video [%d] track video size [%d X %d]\r\n"),i , gVideoWidth,gVideoHeight));
				break;
			}
			else if (video_stream_info.height!=0 && video_stream_info.width!=0)
			{
				gVideoHeight= video_stream_info.height;
				gVideoWidth = video_stream_info.width;
				RETAILMSG(TRUE, (TEXT("AP Set Video [%d] track video size [%d X %d] Codec Id Unknow\r\n"),i , gVideoWidth,gVideoHeight));
				break;
			}
		}
	}
	if (!gVideoHeight&!gVideoWidth)
		gbAudioOnly = TRUE;
}

void OPENMAXAL_SetVideoFullScreen()
{
}

void OPENMAXAL_SetVideoOriginalScreen()
{
	
	XAresult res;
	XARectangle SrcRect;
	XARectangle DestRect;
    XAuint32 vVideoPath = 0; // 0: Main, 1:PIP

	res = (*playerVideoPostProcessItf)->SetScaleOptions(playerVideoPostProcessItf, scaleOption,0,0);
    ASSERT(XA_RESULT_SUCCESS == res);

	SrcRect.left = 0;
	SrcRect.top = 0;
	SrcRect.height = gVideoHeight;
	SrcRect.width = gVideoWidth;

	res = (*playerVideoPostProcessItf)->SetSourceRectangle(playerVideoPostProcessItf, &SrcRect);
    ASSERT(XA_RESULT_SUCCESS == res);

	DestRect.left = 0;
	DestRect.top = 0;
	DestRect.width = VIDEO_WINDOW_WIDTH;
	DestRect.height = VIDEO_WINDOW_HEIGHT;

	res = (*playerVideoPostProcessItf)->SetDestinationRectangle(playerVideoPostProcessItf, &DestRect);
    ASSERT(XA_RESULT_SUCCESS == res);

	res = (*playerVideoPostProcessItf)->SetVideoPath(playerVideoPostProcessItf, vVideoPath);
    ASSERT(XA_RESULT_SUCCESS == res);

	res = (*playerVideoPostProcessItf)->Commit(playerVideoPostProcessItf);
    ASSERT(XA_RESULT_SUCCESS == res);

//	SetParent(gMainVWhwnd,gDiagBoxhWnd);
//    SetWindowPos(gMainVWhwnd, HWND_TOPMOST, 0, 0, VIDEO_WINDOW_WIDTH, VIDEO_WINDOW_HEIGHT,  SWP_NOOWNERZORDER);
}

void OPENMAXAL_SetVideoWideScreen()
{
}

void OPENMAXAL_SetPlaybackRepeat(BOOL bEnable)
{
}

void OPENMAXAL_SetupMetaData()
{
	XAresult res;
	XAuint32 mdCount = 0;
	XAuint32 i;
	/* scan through the metadata items */
	res = (*metadataExtractionItf)->GetItemCount(metadataExtractionItf, &mdCount);
    ASSERT(XA_RESULT_SUCCESS == res);
	for (i = 0; i < mdCount;i++)
	{
		XAMetadataInfo * key = NULL;
		XAMetadataInfo * value = NULL;
		XAuint32 itemSize = 0;
		/* get the size of and malloc memory for the metadata item */
		res = (*metadataExtractionItf)->GetKeySize(metadataExtractionItf,i, &itemSize);
        ASSERT(XA_RESULT_SUCCESS == res);
		key = (XAMetadataInfo*)malloc(itemSize);
		if (key) /* no malloc error */
		{
			/* extract the key into the memory */
			res = (*metadataExtractionItf)->GetKey(metadataExtractionItf,i, itemSize, key);
            ASSERT(XA_RESULT_SUCCESS == res);
			if (key->encoding == XA_CHARACTERENCODING_ASCII)
			{
				res = (*metadataExtractionItf)->GetValueSize(metadataExtractionItf,i, &itemSize);
                ASSERT(XA_RESULT_SUCCESS == res);
				value = (XAMetadataInfo*)malloc(itemSize);
				if (value) /* no malloc error */
				{
					/* extract the value into the memory */
					res = (*metadataExtractionItf)->GetValue(metadataExtractionItf,i, itemSize, value);
                    ASSERT(XA_RESULT_SUCCESS == res);
					if (value->encoding == XA_CHARACTERENCODING_ASCII)
					{
                        TCHAR szTmpKey[64];
                        TCHAR szTmpVal[64];
                        MByteToWChar((LPCSTR)key->data,szTmpKey,sizeof(szTmpKey)/sizeof(szTmpKey[0]),FALSE);
                        MByteToWChar((LPCSTR)value->data,szTmpVal,sizeof(szTmpVal)/sizeof(szTmpVal[0]),FALSE);
                        //OALLog(L"Meta Data index = %d, key = %s,value = %s\r\n",i,szTmpKey,szTmpVal);
                        //MessageBox(NULL,szTmpKey,_T("hello"),MB_OK);
                        //MessageBox(NULL,szTmpVal,_T("hello"),MB_OK);
					}
                    if (value->encoding == XA_CHARACTERENCODING_UTF8)
					{
                        TCHAR szTmpKey[64];
                        TCHAR szTmpVal[64];
                        MByteToWChar((LPCSTR)key->data,szTmpKey,sizeof(szTmpKey)/sizeof(szTmpKey[0]),TRUE);
                        MByteToWChar((LPCSTR)value->data,szTmpVal,sizeof(szTmpVal)/sizeof(szTmpVal[0]),TRUE);
                        //OALLog(L"Meta Data index = %d, key = %s,value = %s\r\n",i,szTmpKey,szTmpVal);
                        //MessageBox(NULL,szTmpKey,_T("hello"),MB_OK);
                        //MessageBox(NULL,szTmpVal,_T("hello"),MB_OK);
					}
					free(value);
				}
			}
			free(key);
		}
	}
}
void OPENMAXAL_SetupAudioTrack()
{
}

void OPENMAXAL_DestroyMediaPlayerObj()
{
	OPENMAXAL_StopFile();
	(*playerObj)->Destroy(playerObj);


//	DestroyWindow(gMainVWhwnd);
//	DestroyWindow(gSubVWhwnd);
//	gMainVWhwnd = NULL;
//	gSubVWhwnd = NULL;
	gPlayerState = ePlayer_init;
	gMute = FALSE;
	gbPlaybackLoop = FALSE;
	gDuration = 0;
//	StopTimer();
	gTimerID=0;
	gbCanSeek = TRUE;
	gVideoWidth = 0;
	gVideoHeight = 0;
	gbFullscreen = FALSE;
	gbWidescreen = FALSE;
	gbPlayerDestroy = TRUE;
	gbAudioOnly = FALSE;
	gbVideoOutputSubSreen = FALSE;
	gbVideoOutputMainSreen = TRUE;
	gbSubtitleEnable = FALSE;
	scaleOption = XA_VIDEOSCALE_STRETCH;
	playerObj = NULL;
	playerPlayItf = NULL;
	playerStreamInfoItf = NULL;
	playerSeekItf = NULL;
	playerVolItf = NULL;
	playerVideoPostProcessItf = NULL;
	gbSubScreenOutputFormatPAL = TRUE;
}







